package com.example.demo.service.impl;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileReader;
import com.example.demo.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedReader;

/**
 * @author zhanpengguo
 * @date 2019-07-09 16:45
 */
@Service
@Slf4j
public class FileServiceImpl implements FileService {
    @Override
    public void download(HttpServletResponse response) {
        FileReader fileReader = new FileReader("D:\\books\\modern-java.pdf");
        // content-type参考：http://tool.oschina.net/commons
        response.setContentType("application/pdf");
        try (
                BufferedInputStream inputStream = fileReader.getInputStream();
                ServletOutputStream outputStream = response.getOutputStream();
        ) {
            IoUtil.copy(inputStream,outputStream);
        }catch (Exception e){
            log.error("读取文件错误",e);
        }
    }
}
